<template>
  <div>
    <el-card style="margin: 20px 0px">
      <CategorySelect
        @getCategoryId="getCategoryId"
        :show="scene != 0"
      ></CategorySelect>
    </el-card>
    <el-card>
      <!-- 展示SPU列表 -->
      <div v-show="scene == 0">
        <el-button type="primary" icon="el-icon-plus" @click="addSpu"
          >添加SPU</el-button
        >
        <el-table :data="spuList" style="width: 100%" border>
          <el-table-column
            label="序号"
            width="82"
            align="center"
            type="index"
          ></el-table-column>
          <el-table-column prop="spuName" label="SPU名称" width="width">
          </el-table-column>
          <el-table-column prop="description" label="SPU概述" width="width">
          </el-table-column>
          <el-table-column prop="prop" label="操作" width="width">
            <template slot-scope="{ row }">
              <hint-button
                type="success"
                icon="el-icon-plus"
                size="mini"
                title="添加sku"
                @click="addSku(row)"
              ></hint-button>
              <hint-button
                type="warning"
                icon="el-icon-edit"
                size="mini"
                title="编辑spu"
                @click="editSpu(row)"
              ></hint-button>
              <hint-button
                type="info"
                icon="el-icon-info"
                size="mini"
                title="查看当前spu全部sku列表"
                @click="handler(row)"
              ></hint-button>
              <el-popconfirm
                :title="`你确定删除${row.spuName}吗？`"
                @onConfirm="deleteSpu(row)"
              >
                <hint-button
                  type="danger"
                  icon="el-icon-delete"
                  size="mini"
                  title="删除spu"
                  slot="reference"
                ></hint-button>
              </el-popconfirm>
            </template>
          </el-table-column>
        </el-table>
      </div>
      <SpuForm
        v-show="scene == 1"
        ref="spu"
        @changeScene="changeScene"
      ></SpuForm>
      <SkuForm
        v-show="scene == 2"
        ref="sku"
        @changeScene="changeScene"
      ></SkuForm>
    </el-card>
    <el-dialog
      :title="`${spuName}的sku列表`"
      :visible.sync="dialogTableVisible"
       v-loading="loading"
       :before-close="close"
    >
      <el-table :data="skuList" style="width: 100%" >
        <el-table-column prop="skuName" label="名称" width="width">
        </el-table-column>
        <el-table-column prop="price" label="价格" width="width">
        </el-table-column>
        <el-table-column prop="weight" label="重量" width="width">
        </el-table-column>
        <el-table-column label="默认图片" width="width">
          <template slot-scope="{row}">
            <img :src="row.skuDefaultImg" style="width:82px;height:82px">
          </template>
        </el-table-column>
      </el-table>
    </el-dialog>
    <el-pagination
      style="text-align: center"
      :current-page="page"
      :page-sizes="[3, 5, 7]"
      :page-size="limit"
      layout="prev, pager, next,->,sizes, total"
      :total="total"
      @size-change="handleSizeChange"
      @current-change="handleCurrentChange"
    >
    </el-pagination>
  </div>
</template>

<script>
import SkuForm from "./SkuForm.vue";
import SpuForm from "./SpuForm.vue";
export default {
  name: "Spu",
  components: {
    SkuForm,
    SpuForm,
  },
  data() {
    return {
      category1Id: "",
      category2Id: "",
      category3Id: "",
      spuList: [],
      page: 1,
      limit: 3,
      total: 0,
      scene: 0, //0代表展示spu列表数据，1代表添加SPU||修改SPU，2添加SKU
      dialogTableVisible: false, //显示隐藏对话框
      spuName: "",
      skuList: [],
      loading:true
    };
  },
  methods: {
    // 通过子组件获取相应的id
    getCategoryId({ categoryId, level }) {
      if (level == 1) {
        this.category1Id = categoryId;
        this.category2Id = "";
        this.category3Id = "";
      } else if (level == 2) {
        this.category2Id = categoryId;
        this.category3Id = "";
      } else {
        this.category3Id = categoryId;
        this.getSpuList();
      }
    },
    //获取spu数据
    async getSpuList() {
      const { page, limit, category3Id } = this;
      let result = await this.$API.spu.reqGetSpuList(page, limit, category3Id);
      if (result.code == 200) {
        this.spuList = result.data.records;
        this.total = result.data.total;
      }
    },
    //改变当前页
    handleCurrentChange(page) {
      this.page = page;
      this.getSpuList();
    },
    //改变一页的数据个数
    handleSizeChange(limit) {
      this.limit = limit;
      this.getSpuList();
    },
    //添加spu
    addSpu() {
      this.scene = 1;
      this.$refs.spu.addSpuData(this.category3Id);
    },
    //编辑spu
    editSpu(row) {
      this.scene = 1;
      this.$refs.spu.initSpuData(row);
    },
    //删除spu
    async deleteSpu(row) {
      let result = await this.$API.spu.reqDeleteSpu(row.id);
      this.getSpuList();
    },
    changeScene(scene) {
      this.scene = scene;
      this.getSpuList();
    },
    //添加sku
    addSku(row) {
      this.scene = 2;
      this.$refs.sku.getData(this.category1Id, this.category2Id, row);
    },
    //查看sku列表
    async handler(spu) {
      this.dialogTableVisible = true;
      this.spuName = spu.spuName;
      let result = await this.$API.spu.reqSkuList(spu.id);
      if (result.code == 200) {
        this.skuList = result.data;
        this.loading=false
      }
    },
    //关闭dialog
    close(done){
      done()
      this.skuList=[]
      this.loading=true
    }
  },
};
</script>

<style>
</style>